package com.wanpinghui.supplier.widgets;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.utils.FileUtil;
import com.wanpinghui.supplier.utils.OpenFileUtil;
import com.wanpinghui.wphlibrary.model.db.manager.MsgDbManager;
import com.wanpinghui.wphlibrary.model.message.bean.MessagesExt;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBean;
import com.yunshl.yunshllibrary.utils.NumberUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;

import java.io.File;

/**
 * Created by xiongning on 17-12-7.
 */

public class MsgRightDocView extends LinearLayout {

    private RelativeLayout rl_msg_content;

    private TextView tv_doc_name;

    private TextView tv_doc_size;

    private ImageView iv_type;

    private SpinView sv;

    private ImageView iv_error;

    private MsgBean msgBean;

    public MsgRightDocView(Context context) {
        super(context);
        init(context);
    }

    public MsgRightDocView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MsgRightDocView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MsgRightDocView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void open(String filePath) {
        Intent intent = OpenFileUtil.openFile(filePath);
        if (intent != null) {
            FileUtil.getActivity().startActivity(intent);
        } else {
            FileUtil.getActivity().showWarningText("打开失败");
        }
    }

    private void onOpenFile() {
        MessagesExt ext = MessagesExt.getMessagesExt(msgBean.getExt());
        if (TextUtil.isNotEmpty(ext.getFile_path()) && new File(ext.getFile_path()).exists()) {
            open(ext.getFile_path());
        } else {
            FileUtil.downLoadFile(msgBean.getMsg(), new FileUtil.OnDownListener() {
                @Override
                public void onResult(String path, boolean isSuccess) {
                    if (isSuccess) {
                        MessagesExt ext = MessagesExt.getMessagesExt(msgBean.getExt());
                        ext.setFile_path(path);
                        MsgDbManager.getInstance().saveMsbBean(msgBean);
                        open(path);
                    } else {
                        FileUtil.getActivity().showWarningText("打开失败");
                    }
                }
            });
        }
    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View mView = inflater.inflate(R.layout.item_msg_right_doc, this);
        rl_msg_content = (RelativeLayout) mView.findViewById(R.id.rl_msg_content);
        tv_doc_name = (TextView) mView.findViewById(R.id.tv_doc_name);
        tv_doc_size = (TextView) mView.findViewById(R.id.tv_doc_size);
        iv_type = (ImageView) mView.findViewById(R.id.iv_type);
        sv = (SpinView) mView.findViewById(R.id.sv);
        iv_error = (ImageView) mView.findViewById(R.id.iv_error);
        rl_msg_content.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onOpenFile();
            }
        });
    }

    public void initData(MsgBean msgBean) {
        this.msgBean = msgBean;
        MessagesExt ext = MessagesExt.getMessagesExt(msgBean.getExt());
        setName(ext.getFile_name());
        setSize(ext.getFile_len());
        setType(ext.getFile_type());
        switch (msgBean.getSend_statu()) {
            case 0:
                setError(false);
                setSending(true);
                break;
            case 1:
                setError(false);
                setSending(false);
                break;
            case 2:
                setError(true);
                setSending(false);
                break;
        }
    }

    public void setType(String type) {
        if (TextUtil.isEmpty(type)) {

        } else if (type.equals("ppt") || type.equals("pptx")) {
            iv_type.setImageResource(R.mipmap.message_icon_adjunct_ppt);
        } else if (type.equals("xls") || type.equals("xlsx")) {
            iv_type.setImageResource(R.mipmap.message_icon_adjunct_excle);
        } else if (type.equals("pdf")) {
            iv_type.setImageResource(R.mipmap.message_icon_adjunct_pdf);
        } else if (type.equals("doc") || type.equals("docx")) {
            iv_type.setImageResource(R.mipmap.message_icon_adjunct_word);
        }
    }

    public void setName(String name) {
        tv_doc_name.setText(name);
    }

    public void setSize(long size) {
        double d = size / (1024.0 * 1024.0d);
        if (d < 0.01) {
            d = 0.01;
        }
        tv_doc_size.setText(NumberUtil.double2String(d) + "M");
    }

    public void setSending(boolean isSendding) {
        if (isSendding)
            sv.setVisibility(View.VISIBLE);
        else
            sv.setVisibility(View.GONE);

    }

    public void setError(boolean isError) {
        if (isError)
            iv_error.setVisibility(View.VISIBLE);
        else
            iv_error.setVisibility(View.GONE);
    }
}
